﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data
{
    public partial class FrmInstallationInformation : Form
    {
        DataBase userDB = new DataBase();
        public FrmInstallationInformation()
        {
            InitializeComponent();
            //统一页面窗口的属性，例如窗口打开都是页面最中间
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }

        private void FrmInstallationInformation_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“大数据DataSet.T_assemble”中。您可以根据需要移动或删除它。
            //this.t_assembleTableAdapter.Fill(this.大数据DataSet.T_assemble);
            // TODO: 这行代码将数据加载到表“大数据DataSet.T_assemble”中。您可以根据需要移动或删除它。
            //this.t_assembleTableAdapter.Fill(this.大数据DataSet.T_assemble);
            // TODO: 这行代码将数据加载到表“大数据DataSet2.T_assemble”中。您可以根据需要移动或删除它。
            // this.t_assembleTableAdapter.Fill(this.大数据DataSet.T_assemble);

            // 大数据DataSet dataSet = new 大数据DataSet();//1
            //DataTable dataTable = dataSet.T_assemble;//4
            this.BindInstallInformationData();

        }

        private void BindInstallInformationData()
        {
            var sta_code = this.txtSta_code.Text;
            string sqlstr = "select * from T_assemble ";//2
            if (!string.IsNullOrEmpty(sta_code))
            {
                sqlstr += " where Ass_no = '" + this.txtSta_code.Text + "' ";
            }
            DataTable dataTable = userDB.show(sqlstr);//3
            this.dgvInstallationInformation.DataSource = dataTable;//5
        }

        private void BindInstallInformationDetailsData(string str_ass_no)
        {
            string sqlstr = "select * from T_assemble_sub where Ass_no='" + str_ass_no + "'";//2
            DataTable dataTable = userDB.show(sqlstr);//3
            this.dgwDetails.DataSource = dataTable;//5
        }

        // DGV直接编辑修改数据的功能
        /// <summary>
        /// 用来存放dgv单元格修改之前值
        /// </summary>
        Object cellTempValue = null;

        /// <summary>
        /// DGV单元格开始编辑时触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            cellTempValue = dgvInstallationInformation.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        }

        /// <summary>
        /// DGV单元格结束编辑时触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //判断编辑前后的值是否一样（是否修改了内容）
            if (Object.Equals(cellTempValue, dgvInstallationInformation.Rows[e.RowIndex].Cells[e.ColumnIndex].Value))
            {
                //如果没有修改，则返回
                return;
            }

            //判断用户是否确定修改
            if (MessageBox.Show("确定修改?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.None) != DialogResult.OK)
            {
                //如果不修改，恢复原来的值
                dgvInstallationInformation.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = cellTempValue;
                return;
            }

            //修改数据库数据
            string sql = String.Format("update T_assemble set {0} = '{1}' where {2} ='{3}'",
                 dgvInstallationInformation.Columns[this.dgvInstallationInformation.CurrentCell.ColumnIndex].DataPropertyName,   //所选单元格列名
                dgvInstallationInformation.Rows[e.RowIndex].Cells[e.ColumnIndex].Value,                 //所选单元格修改
                dgvInstallationInformation.Columns[0].DataPropertyName,                    //所选单元格列名
                 dgvInstallationInformation.Rows[e.RowIndex].Cells[0].Value);               //所选行的商品


            try
            {
                int n = userDB.dosqlcom(sql);
                string msg = n > 0 ? "操作成功" : "操作失败";
                MessageBox.Show(msg);
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
            }

            //刷新数据
            //FrmInstallationInformation_Load();
        }

        private void tassembleBindingSource_CurrentChanged_1(object sender, EventArgs e)
        {


        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.BindInstallInformationData();
        }

        /// <summary>
        /// 行选中事件切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvInstallationInformation_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgvInstallationInformation.SelectedRows.Count == 0)
            {
                return;
            }
            DataGridViewRow currentRow = this.dgvInstallationInformation.CurrentRow;
            var sAss_no = currentRow.Cells[0].Value.ToString();
            this.BindInstallInformationDetailsData(sAss_no);
        }

        /// <summary>
        /// 字表删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //判断用户是否确定修改
            if (MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.None) == DialogResult.OK)
            {
                if (this.dgwDetails.SelectedRows.Count == 0)
                {
                    MessageBox.Show("请至少选中一条数据进行删除！");
                    return;
                }
                DataGridViewRow currentRow = this.dgwDetails.CurrentRow;
                var sAss_no = currentRow.Cells[1].Value.ToString();
                var sAss_no_sub = currentRow.Cells[2].Value.ToString();
                string sqlstr = "Delete from T_assemble_sub where Ass_no_sub='" + sAss_no_sub + "' and  Ass_no='" + sAss_no + "'";//2
                                                                                                                                  //如果删除的数据行数大于0，则表示删除成功
                if (userDB.dosqlcom(sqlstr) > 0)
                {
                    BindInstallInformationDetailsData(sAss_no);
                    MessageBox.Show("删除成功！");
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        /// <summary>
        /// 字表新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenDetailEditForm("");
        }

        private void dgwDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            if (CIndex == 0)
            {
                DataGridViewRow selectRow = this.dgwDetails.Rows[e.RowIndex];
                var sAss_no_sub = selectRow.Cells[1].Value.ToString();
                OpenDetailEditForm(sAss_no_sub);
            }
        }

        private void OpenDetailEditForm(string sAss_no_sub)
        {
            if (this.dgvInstallationInformation.SelectedRows.Count == 0)
            {
                return;
            }
            DataGridViewRow currentRow = this.dgvInstallationInformation.CurrentRow;
            //获取主表选中行数据主键
            var sAss_no = currentRow.Cells[0].Value.ToString();

            //弹出新增窗口
            View.FrmInstallInformationDetailEdit edit = new View.FrmInstallInformationDetailEdit(sAss_no_sub, sAss_no);
            if (edit.ShowDialog() == DialogResult.OK)
            {
                this.BindInstallInformationDetailsData(sAss_no);
            }
        }

        private void OpenInstallInfoEditForm(string _ass_no)
        {
            //弹出新增窗口
            View.FrmInstallInformationEdit edit = new View.FrmInstallInformationEdit(_ass_no);
            if (edit.ShowDialog() == DialogResult.OK)
            {
                this.BindInstallInformationData();
            }
        }

        private void btnEditMaster_Click(object sender, EventArgs e)
        {
            this.OpenInstallInfoEditForm("");
        }

        private void dgvInstallationInformation_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            if (this.dgvInstallationInformation.Columns["btnInstallEdit"].Index == CIndex)
            {
                DataGridViewRow selectRow = this.dgvInstallationInformation.Rows[e.RowIndex];
                var sAss_no_sub = selectRow.Cells[0].Value.ToString();
                OpenInstallInfoEditForm(sAss_no_sub);
            }
            else
            {
                if (this.dgvInstallationInformation.SelectedRows.Count == 0)
                {
                    return;
                }
                DataGridViewRow currentRow = this.dgvInstallationInformation.CurrentRow;
                var sAss_no = currentRow.Cells[0].Value.ToString();
                this.BindInstallInformationDetailsData(sAss_no);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //判断用户是否确定修改
            if (MessageBox.Show("确定要删除安装记录和明细记录吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.None) == DialogResult.OK)
            {
                if (this.dgvInstallationInformation.SelectedRows.Count == 0)
                {
                    MessageBox.Show("请至少选中一条数据进行删除！");
                    return;
                }
                DataGridViewRow currentRow = this.dgvInstallationInformation.CurrentRow;
                var sAss_no = currentRow.Cells[0].Value.ToString();
                string sqlstr = "Delete from T_assemble_sub where Ass_no='" + sAss_no + "'";//2
                userDB.dosqlcom(sqlstr);                                                                   //如果删除的数据行数大于0，则表示删除成功
                sqlstr = "Delete from T_assemble  where Ass_no='" + sAss_no + "'";
                if (userDB.dosqlcom(sqlstr) > 0)
                {
                    BindInstallInformationData();
                    MessageBox.Show("删除成功！");
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }
    }
}
